<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/08/07 05:20:29 $)
// $RCSfile: functions_jscache.php,v $ : $Revision: 1.9 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================



/**
* Convert PHP string data to JS string output
*/
function JSdataToString($stringname, $data)
{
	$data = preg_replace("#(\r\n|\r|\n)#s", SWIFT_CRLF, $data); // Convert to universal CRLF

	$jsdata = $stringname." = '". str_replace(SWIFT_CRLF, "\\n", addslashes($data)) ."';".SWIFT_CRLF;

	return $jsdata;
}

/**
* Insert to JS Cache file
*/
function insertToJSCache($cachetitle, $jscode)
{
	global $_SWIFT;

	$prefix = "// BEGIN_JSCACHE_CONTENT: $cachetitle";
	$suffix = "// END_JSCACHE_CONTENT: $cachetitle";

	$jscachefile = $_SWIFT["settingscache"]["fileid"]["jscachefile"];
	// Get the file contents
	$filepath = "./files/".$jscachefile;

	// Get the file contents
	$filedata = @file($filepath);
	if (!$filedata)
	{
		$filedata = array();
	}
	$codecontents = $prefix.SWIFT_CRLF.$jscode.SWIFT_CRLF.$suffix.SWIFT_CRLF;

	$finaldata = array();
	$append = true;
	foreach ($filedata as $key=>$val)
	{
		if (trim($val) == $prefix)
		{
			$append = false;
		} else if (trim($val) == $suffix) {
			$append = true;
		}else if ($append == true) {
			$finaldata[] = trim($val);
		}
	}
	$towrite = implode(SWIFT_CRLF, $finaldata);
	$towrite .= SWIFT_CRLF.$codecontents;
	$fp = fopen($filepath, "w+");
	fwrite($fp, trim($towrite));
	fclose($fp);
	@chmod($filepath, 0666);

	return true;
}

?>